package com.hframe.typefilter;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 将时间字符串转换成日期和时间 yyyy/MM/dd HH:mm:ss
 *
 * @author Administrator
 */
public class StringToDate implements FilterRef.Date<java.util.Date, Object> {
    private static final Logger log = Logger.getLogger(StringToDate.class);
    private String parse = DATE_TIME_LINE;

    public StringToDate() {
    }

    /**
     * 时间格式
     *
     * @param format
     */
    public StringToDate(String format) {
        this.parse = format;
    }

    @Override
    public java.util.Date filter(Object object) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(parse);
            return sdf.parse((String) object);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    public static java.util.Date parse(String object, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(object);
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
}